package com.imooc.controller;

import com.imooc.pojo.Users;
import com.imooc.pojo.bo.UserBO;
import com.imooc.service.UserService;
import com.imooc.utils.CookieUtils;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/passport")
@Api(value = "注册登录",tags = {"passport,用于注册和登录的相关接口"})
public class PassportController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "判断用户名是否存在",notes ="判断用户名是否存在",httpMethod = "GET")
    @GetMapping("/usernameIsExist")
    public IMOOCJSONResult userNameIsExist(@RequestParam String username){

        if (StringUtils.isEmpty(username)){
            return IMOOCJSONResult.errorMsg("用户名不能为空");
        }
        if (userService.queryNameIsExist(username)){
            return IMOOCJSONResult.errorMsg("用户名已经存在");
        }else{
            return IMOOCJSONResult.ok();
        }
    }

    @ApiOperation(value = "注册用户",notes ="注册用户",httpMethod = "POST")
    @PostMapping("/regist")
    public IMOOCJSONResult regist(@RequestBody UserBO userBO){

        String username=userBO.getUsername();
        String password=userBO.getPassword();
        String confirmPwd=userBO.getConfirmPassword();

        //0.判断用户名和密码必须不为空

        if (StringUtils.isEmpty(username) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(confirmPwd)){
            return IMOOCJSONResult.errorMsg("用户名和密码不能为空");
        }

        //1.查询用户是否存在
        if (userService.queryNameIsExist(username)){
            return IMOOCJSONResult.errorMsg("用户名已经存在");
        }

        //2.密码长度不能少于6位
        if (password.length()<6 || confirmPwd.length()<6){
            return IMOOCJSONResult.errorMsg("密码长度不能少于6位");
        }

        //3.判断密码两次是否一致
        if (!password.equals(confirmPwd)){
            return IMOOCJSONResult.errorMsg("两次密码输入不一致");
        }

        //4.实现注册

        userService.createUser(userBO);


        return IMOOCJSONResult.ok();

    }

    @ApiOperation(value = "登录功能",notes ="判断是否登出用户",httpMethod = "POST")
    @PostMapping("/login")
    public IMOOCJSONResult login(@RequestBody UserBO userBO,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {

        String username=userBO.getUsername();
        String password=userBO.getPassword();

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            return IMOOCJSONResult.errorMsg("用户名或密码不能为空");
        }
        Users users=userService.loginUser(username, MD5Utils.getMD5Str(password));
        if (users==null){
            return IMOOCJSONResult.errorMsg("用户名或密码不正确");
        }

        users=setNullProperty(users);

        CookieUtils.setCookie(request,response,"user"
                , JsonUtils.objectToJson(users),true);


        //TODO 生成用户token，存入redis回话
        //TODO 同步购物车数据

        return IMOOCJSONResult.ok(users);

    }

    private Users setNullProperty(Users users){
        users.setPassword(null);
        users.setMobile(null);
        users.setEmail(null);
        users.setCreatedTime(null);
        users.setUpdatedTime(null);
        users.setBirthday(null);
        return users;
    }

    @ApiOperation(value = "登出功能",notes ="用户退出登录",httpMethod = "POST")
    @PostMapping("/logout")
    public IMOOCJSONResult logout(@RequestParam String userId,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {

        //清除用户相关信息的cookie
        CookieUtils.deleteCookie(request,response,"user");

        // TODO 用户退出登录，需要清空购物车
        // TODO 分布式会话中需要清除用户数据

        return IMOOCJSONResult.ok();

    }


}
